/*
 * PanelTeman.java
 *
 * Created on 10 April 2008, 0:57
 */
package usu.buku.telepon.admin;

import java.awt.event.ActionListener;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import usu.buku.telepon.widget.Table;
import usu.buku.telepon.widget.table.TableModelTeman;
import usu.buku.telepon.widget.table.render.DefaultTableCellRender;

/**
 *
 * @author  usu
 */
public class PanelTeman extends javax.swing.JPanel {

    private static final long serialVersionUID = -1;
    private TableModelTeman tableModel;

    /** Creates new form PanelTeman */
    public PanelTeman() {
        initVariables();
        initComponents();
        initFinal();
    }

    /**
     * 
     * @param l
     */
    public void addActionListenerSaring(ActionListener l) {
        buttonSaring.addActionListener(l);
    }

    /**
     * 
     * @param l
     */
    public void addActionListenerSegarkan(ActionListener l) {
        buttonSegarkan.addActionListener(l);
    }

    /**
     * 
     * @param l
     */
    public void addActionListenerTambah(ActionListener l) {
        buttonTambah.addActionListener(l);
    }

    /**
     * 
     * @param l
     */
    public void addActionListenerUbah(ActionListener l) {
        buttonUbah.addActionListener(l);
    }

    /**
     * 
     * @param l
     */
    public void addActionListenerHapus(ActionListener l) {
        buttonhapus.addActionListener(l);
    }

    void reset() {
        textCari.setText("");
        textNama.setText("");
        textTelepon.setText("");
    }

    @Override
    public void setEnabled(boolean enabled) {
        buttonSaring.setEnabled(enabled);
        buttonSaring.setEnabled(enabled);
        buttonTambah.setEnabled(enabled);
        buttonUbah.setEnabled(enabled);
        buttonhapus.setEnabled(enabled);
        textCari.setEnabled(enabled);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        usu.buku.telepon.widget.Label label1 = new usu.buku.telepon.widget.Label();
        textCari = new usu.buku.telepon.widget.TextBox();
        usu.buku.telepon.widget.ScrollPane scrollPane1 = new usu.buku.telepon.widget.ScrollPane();
        table = new usu.buku.telepon.widget.Table();
        usu.widget.glass.PanelGlass panelGlass1 = new usu.widget.glass.PanelGlass();
        usu.buku.telepon.widget.Label label2 = new usu.buku.telepon.widget.Label();
        usu.buku.telepon.widget.Label label3 = new usu.buku.telepon.widget.Label();
        textNama = new usu.buku.telepon.widget.TextBox();
        textTelepon = new usu.buku.telepon.widget.TextBox();
        javax.swing.JCheckBox jCheckBox1 = new javax.swing.JCheckBox();
        javax.swing.JCheckBox jCheckBox2 = new javax.swing.JCheckBox();
        buttonSegarkan = new usu.buku.telepon.widget.Button();
        buttonSaring = new usu.buku.telepon.widget.Button();
        buttonhapus = new usu.buku.telepon.widget.Button();
        buttonUbah = new usu.buku.telepon.widget.Button();
        buttonTambah = new usu.buku.telepon.widget.Button();

        setOpaque(false);

        label1.setDisplayedMnemonic('C');
        label1.setText("cari (case sensitive) :");

        textCari.setFocusAccelerator('C');

        scrollPane1.setViewportView(table);

        panelGlass1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        panelGlass1.setOpaqueImage(false);
        panelGlass1.setRound(false);

        label2.setDisplayedMnemonic('N');
        label2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        label2.setText("nama :");

        label3.setDisplayedMnemonic('P');
        label3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        label3.setText("telepon :");

        textNama.setFocusAccelerator('N');
        textNama.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textNamaActionPerformed(evt);
            }
        });

        textTelepon.setFocusAccelerator('P');
        textTelepon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textTeleponActionPerformed(evt);
            }
        });

        jCheckBox1.setFocusable(false);
        jCheckBox1.setOpaque(false);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, textNama, org.jdesktop.beansbinding.ELProperty.create("${enabled}"), jCheckBox1, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        jCheckBox2.setFocusable(false);
        jCheckBox2.setOpaque(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, textTelepon, org.jdesktop.beansbinding.ELProperty.create("${enabled}"), jCheckBox2, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout panelGlass1Layout = new javax.swing.GroupLayout(panelGlass1);
        panelGlass1.setLayout(panelGlass1Layout);
        panelGlass1Layout.setHorizontalGroup(
            panelGlass1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelGlass1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelGlass1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(label2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(label3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelGlass1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textTelepon, javax.swing.GroupLayout.DEFAULT_SIZE, 554, Short.MAX_VALUE)
                    .addComponent(textNama, javax.swing.GroupLayout.DEFAULT_SIZE, 554, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelGlass1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox1)
                    .addComponent(jCheckBox2))
                .addContainerGap())
        );
        panelGlass1Layout.setVerticalGroup(
            panelGlass1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelGlass1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelGlass1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelGlass1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textTelepon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonSegarkan.setMnemonic('S');
        buttonSegarkan.setText("segarkan");

        buttonSaring.setMnemonic('G');
        buttonSaring.setText("saring");

        buttonhapus.setMnemonic('H');
        buttonhapus.setText("hapus");

        buttonUbah.setMnemonic('H');
        buttonUbah.setText("ubah");

        buttonTambah.setMnemonic('A');
        buttonTambah.setText("tambah");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCari, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(buttonSegarkan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 273, Short.MAX_VALUE)
                        .addComponent(buttonTambah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonUbah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonhapus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonSaring, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(panelGlass1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelGlass1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSegarkan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonSaring, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonhapus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonUbah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonTambah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

private void textNamaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textNamaActionPerformed
    textTelepon.requestFocusInWindow();
}//GEN-LAST:event_textNamaActionPerformed

private void textTeleponActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textTeleponActionPerformed
    buttonTambah.requestFocusInWindow();
}//GEN-LAST:event_textTeleponActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    usu.buku.telepon.widget.Button buttonSaring;
    usu.buku.telepon.widget.Button buttonSegarkan;
    usu.buku.telepon.widget.Button buttonTambah;
    usu.buku.telepon.widget.Button buttonUbah;
    usu.buku.telepon.widget.Button buttonhapus;
    usu.buku.telepon.widget.Table table;
    usu.buku.telepon.widget.TextBox textCari;
    usu.buku.telepon.widget.TextBox textNama;
    usu.buku.telepon.widget.TextBox textTelepon;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void initFinal() {
        table.setModel(tableModel);

        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellRenderer(new DefaultTableCellRender());
        }

        textCari.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                filterTable(textCari.getText());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                filterTable(textCari.getText());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                filterTable(textCari.getText());
            }
        });
    }
    // End of variables declaration

    private void initVariables() {
        tableModel = new TableModelTeman();
    }

    /**
     * 
     * @param text
     */
    @SuppressWarnings("unchecked")
    public void filterTable(String text) {
        if (text.trim().equals("")) {
            ((TableRowSorter<TableModel>) table.getRowSorter()).setRowFilter(null);
        } else {
            ((TableRowSorter<TableModel>) table.getRowSorter()).setRowFilter(RowFilter.regexFilter(text));
        }
    }

    /**
     * 
     * @return
     */
    public String getNama() {
        if (textNama.isEnabled()) {
            return textNama.getText();
        }
        return null;
    }

    /**
     * 
     * @return
     */
    public String getTelepon() {
        if (textTelepon.isEnabled()) {
            return textTelepon.getText();
        }
        return null;
    }

    /**
     * 
     * @return
     */
    public Table getTable() {
        return table;
    }

    /**
     * 
     * @return
     */
    public TableModelTeman getTableModel() {
        return tableModel;
    }
}
